package com.yanqu.road.server.manger.config.grandchild.talent.trigger;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResult;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResultBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillParamBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillResult;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffect;
import com.yanqu.road.server.manger.config.grandchild.talent.utils.TalentUtils;

import java.util.ArrayList;
import java.util.List;

public class TalentTriggerTOM implements ITalentTrigger {

    @Override
    public TalentTriggerResult getResult(TalentTriggerParam param) {
        List<TalentTriggerSkillResult> skillResultList = new ArrayList<>();

        List<Integer> list = TalentUtils.calcFlairList(param.getGrandChild(), param.getLastTriggerList());
        int maxFlair = list.stream().mapToInt(Integer::intValue).max().getAsInt();

        // 最高天资的职业会有多个
        List<Integer> value1List = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int flair = list.get(i);
            if (flair == maxFlair) {
                value1List.add(i + 1); // 职业 = 天资下标 + 1
            }
        }

        // 只要一个满足就算满足
        boolean can = false;
        int value2 = param.getCondition().getValue();
        for (int value1 : value1List) {
            if (param.getCondition().getFlag().getResult(value1, value2)) {
                can = true;
                break;
            }
        }
        if (can) {

            // 技能生效
            List<SkillInfo> skillInfo = TalentUtils.getSkillInfo(param.getTalentInfo().getSkills(), param.getStage());
            for (SkillInfo info : skillInfo) {
                TalentTriggerSkillParam skillParam = TalentTriggerSkillParamBuilder.aTalentTriggerSkillParam()
                        .setGrandChild(param.getGrandChild())
                        .setSkillInfo(info)
                        .setLastTriggerList(param.getLastTriggerList())
                        .build();
                TalentTriggerSkillResult result = eGrandChildSkillEffect.getResult(skillParam);
                skillResultList.add(result);
            }

        }

        return TalentTriggerResultBuilder.aTalentTriggerResult()
                .setTalentId(param.getTalentInfo().getId())
                .setSkillResultList(skillResultList)
                .build();
    }

}
